package com.wen.POIProject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
/**
 * Steps to update excel file will be:
 * 
 * 1. Open excel file in input mode(inputstream)
 * 2. Use POI API and read the excel content
 * 3. Update cell's value using different setCellValue method
 * 4. Close the excel input (inputstream)
 * 5. Open same excel file in output mode (outputstream)
 * 6. Write content of updated workbook in output file
 * 7. Close output excel file
 * 
 * @author wen
 * @version 1.0
 */
public class App3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			FileInputStream file = new FileInputStream(new File("/Users/wen/Documents/test file/update.xls"));
			
			HSSFWorkbook workbook = new HSSFWorkbook(file);
			HSSFSheet sheet = workbook.getSheetAt(0);
			Cell cell = null;
			
			// Update the value of cell
		    cell = sheet.getRow(1).getCell(2);
		    cell.setCellValue(cell.getNumericCellValue() * 2);
		    cell = sheet.getRow(2).getCell(2);
		    cell.setCellValue(cell.getNumericCellValue() * 2);
		    cell = sheet.getRow(3).getCell(2);
		    cell.setCellValue(cell.getNumericCellValue() * 2);
		     
		    file.close();
		     
		    FileOutputStream outFile =new FileOutputStream(new File("/Users/wen/Documents/test file/update.xls"));
		    workbook.write(outFile);
		    outFile.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
